<template>

    <div>
        <div class="header">
            <div class="header-content">
                <img src="../../assets/images/logo.png" alt="">
                <div class="text">
                    <span>晚上好!系统管理员!</span>
                    <span style="color: grey;">今天天气晴朗</span>
                </div>
            </div>
        </div>
        <div class="centent">
            <el-card style="max-width: 480px;min-width: 320px">
                <template #header>
                    <div class="card-header">
                        <span>销售额</span>
                        <el-check-tag checked>季度</el-check-tag>
                    </div>
                </template>
                <div>
                    <div class="card-header span">
                        <span>30000</span>
                        <el-icon size="30" ><GoldMedal  /></el-icon>
                    </div>
                    <div class="card-header span">
                        <span>年度总销售额</span>
                        <span>60000</span>
                    </div>
                </div>
            </el-card>
            <el-card style="max-width: 480px;min-width: 320px">
                <template #header>
                    <div class="card-header">
                        <span>销售额</span>
                        <el-check-tag checked>季度</el-check-tag>
                    </div>
                </template>
                <div>
                    <div class="card-header span">
                        <span>30000</span>
                        <el-icon size="30" ><GoldMedal  /></el-icon>
                    </div>
                    <div class="card-header span">
                        <span>年度总销售额</span>
                        <span>60000</span>
                    </div>
                </div>
            </el-card>
            <el-card style="max-width: 480px;min-width: 320px">
                <template #header>
                    <div class="card-header">
                        <span>销售额</span>
                        <el-check-tag checked>季度</el-check-tag>
                    </div>
                </template>
                <div>
                    <div class="card-header span">
                        <span>30000</span>
                        <el-icon size="30" ><GoldMedal  /></el-icon>
                    </div>
                    <div class="card-header span">
                        <span>年度总销售额</span>
                        <span>60000</span>
                    </div>
                </div>
            </el-card>
        </div>
        <div class="chart-container">
            <!-- 标题区 -->
            <div class="header-title">
                <el-row>
                    <el-col :span="24">
                        <h2 class="title">数据可视化分析平台</h2>
                    </el-col>
                </el-row>
            </div>

            <!-- 图表区 -->
            <el-row :gutter="20" class="chart-row">
                <!-- 修改 :span 使用动态计算 -->
                <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8" v-for="(chart, index) in charts" :key="index">
                    <div class="chart-box">
                        <div class="chart-title">{{ chart.title }}</div>
                        <div :id="chart.id" class="chart"></div>
                    </div>
                </el-col>
            </el-row>
        </div>
    </div>

</template>

<script setup>
import { onMounted, onUnmounted, ref } from 'vue'
import * as echarts from 'echarts'

// 定义图表配置数组
const charts = ref([
    { id: 'pieChart', title: '销售分布', instance: null },
    { id: 'barChart', title: '月度销售额', instance: null },
    { id: 'lineChart', title: '增长趋势', instance: null }
])

// 初始化所有图表
const initCharts = () => {
    charts.value.forEach(chart => {
        if (chart.instance) {
            chart.instance.dispose()
        }
        chart.instance = echarts.init(document.getElementById(chart.id))
        switch (chart.id) {
            case 'pieChart':
                initPieChart(chart.instance)
                break
            case 'barChart':
                initBarChart(chart.instance)
                break
            case 'lineChart':
                initLineChart(chart.instance)
                break
        }
    })
}

// 处理窗口大小变化
const handleResize = () => {
    charts.value.forEach(chart => {
        chart.instance && chart.instance.resize()
    })
}

onMounted(() => {
    initCharts()
    window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
    window.removeEventListener('resize', handleResize)
    charts.value.forEach(chart => {
        chart.instance && chart.instance.dispose()
    })
})

// 初始化饼图
const initPieChart = () => {
    const pieChart = echarts.init(document.getElementById('pieChart'))
    pieChart.setOption({
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            right: 10,
            top: 'center',
            textStyle: {
                color: '#fff'
            }
        },
        series: [{
            type: 'pie',
            radius: ['40%', '70%'],
            itemStyle: {
                borderRadius: 10,
                borderColor: '#fff',
                borderWidth: 2
            },
            data: [
                { value: 1048, name: '产品A' },
                { value: 735, name: '产品B' },
                { value: 580, name: '产品C' },
                { value: 484, name: '产品D' }
            ]
        }]
    })
}

// 初始化柱状图
const initBarChart = () => {
    const barChart = echarts.init(document.getElementById('barChart'))
    barChart.setOption({
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月'],
            axisLine: {
                lineStyle: {
                    color: '#fff'
                }
            }
        },
        yAxis: {
            type: 'value',
            axisLine: {
                lineStyle: {
                    color: '#fff'
                }
            }
        },
        series: [{
            data: [120, 200, 150, 80, 70, 110],
            type: 'bar',
            showBackground: true,
            backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
            }
        }]
    })
}

// 初始化折线图
const initLineChart = () => {
    const lineChart = echarts.init(document.getElementById('lineChart'))
    lineChart.setOption({
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            type: 'category',
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
            axisLine: {
                lineStyle: {
                    color: '#fff'
                }
            }
        },
        yAxis: {
            type: 'value',
            axisLine: {
                lineStyle: {
                    color: '#fff'
                }
            }
        },
        series: [{
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line',
            smooth: true,
            lineStyle: {
                width: 3,
                shadowColor: 'rgba(0,0,0,0.5)',
                shadowBlur: 10
            }
        }]
    })
}
</script>

<style scoped>
/* 头部样式优化 */
.header {
    background: linear-gradient(to right, #2c3e50, #3498db);
    color: white;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.header-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 20px;

    img {
        border-radius: 50%;
        width: 60px;
        border: 3px solid white;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    }

    .text {
        display: flex;
        flex-direction: column;
        gap: 5px;

        span {
            padding: 0;
            &:last-child {
                opacity: 0.8;
                font-size: 0.9em;
            }
        }
    }
}

/* 内容区域样式优化 */
.centent {
    max-width: 1200px;
    margin: 20px auto;
    padding: 0 20px;
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
}

/* 卡片样式优化 */
:deep(.el-card) {
    flex: 1;
    min-width: 300px;
    transition: all 0.3s ease;
    border: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);

    &:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
    }
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;

    span {
        font-size: 16px;
        font-weight: 500;
    }
}

.span {
    margin: 15px 0;
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    span:first-child {
        font-size: 24px;
        font-weight: bold;
        color: #409EFF;
    }
}

/* 图表容器样式优化 */
.chart-container {
    background: linear-gradient(135deg, #1a2980, #26d0ce);
    padding: 30px;
    border-radius: 12px;
    margin: 20px;
}

.header-title {
    margin-bottom: 30px;
    
    .title {
        color: white;
        text-align: center;
        font-size: 28px;
        font-weight: 600;
        margin: 0;
        text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }
}

.chart-box {
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

    .chart-title {
        color: white;
        font-size: 18px;
        margin-bottom: 15px;
        font-weight: 500;
    }

    .chart {
        height: 300px;
        width: 100%;
        min-height: 250px;
        border-radius: 8px;
    }
}

/* 响应式优化 */
@media screen and (max-width: 768px) {
    .centent {
        padding: 0 10px;
    }

    .chart-container {
        padding: 15px;
        margin: 10px;
    }

    .header-title .title {
        font-size: 24px;
    }
}

@media screen and (max-width: 480px) {
    .header-content {
        padding: 15px;
        
        img {
            width: 50px;
        }
    }

    .chart-box {
        padding: 15px;
    }

    .header-title .title {
        font-size: 20px;
    }
}
</style>